home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d12
/
cgazv5n4.arc
/
NUMBER.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1991-09-23
|
911b
|
29 lines
//--- NUMBER.CPP ------------------------- Listing 3 -----------
// Number the lines in a file (simple test of string class)
// by Bruce Eckel. See Listing 1 for copyright information.
//--------------------------------------------------------------
#include "stringc.h"
main(int argc, char * argv[]) {
if(argc < 2) {
fprintf(stderr, "usage: number filename\n");
exit(1);
}
FILE* fp = fopen(argv[1], "r");
if(fp == NULL) {
fprintf(stderr, "could not open %s\n", argv[1]);
exit(1);
}
const bsz = 80; char buf[bsz];
for(int lines = 0;;) {
if(!fgets(buf, bsz, fp)) break; // quit on end-of-file
buf[strlen(buf) -1] = '\0'; // erase terminating newline
string line(buf); // create a string object from the buffer
sprintf(buf, "%d: ", lines);
line.prepend(buf); // add line number
line.print();
lines++;
}
fclose(fp);
}